﻿using CommonBase;
using CommonClass;
using EnvCommonUtility;
using EnvControlLib;
using FileLib;
using InteractiveWhiteboardManagement.Base.ViewModels;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Regions;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Windows;

namespace InteractiveWhiteboardManagement.ViewModels
{
    /// <summary>
    /// 配置管理
    /// </summary>
    public class ViewModel_Configuration : WorkspaceViewModel
    {
        #region 变量

        private DelegateCommand _cmdApply, _cmdCancel, _cmdReset;

        #endregion

        #region 构造函数

        public ViewModel_Configuration(IRegionManager regionManager)
        {
            if (regionManager == null)
                return;

            base.BaseRegionManager = regionManager;
            base.ViewName = InteractiveWhiteboardManagement.Base.Common.ViewNames.InteractiveWhiteboardManagement_Configuration;
            base.HeaderTitle = "配置管理";
            base.HeaderImageSource = string.Format(ConstantString.IMAGE_URL, "Config");
            base.IsShowCloseButton = Visibility.Visible;

            _tabCountMonitorScreen = EnvUtilities.TabCountMonitorScreen;
            _timerMonitorScreen = EnvUtilities.TimerMonitorScreen;
            _timerHeartbeat = EnvUtilities.TimerHeartbeat;
        }

        #endregion

        #region 属性

        private ushort _tabCountMonitorScreen;
        /// <summary>
        /// 监控画面打开的最大个数
        /// </summary>
        public ushort TabCountMonitorScreen
        {
            get { return _tabCountMonitorScreen; }
            set
            {
                if (_tabCountMonitorScreen == value) return;
                _tabCountMonitorScreen = value;
                base.OnPropertyChanged("TabCountMonitorScreen");
            }
        }

        private ushort _timerMonitorScreen;
        /// <summary>
        /// 监控画面更新秒数
        /// </summary>
        public ushort TimerMonitorScreen
        {
            get { return _timerMonitorScreen; }
            set
            {
                if (_timerMonitorScreen == value) return;
                _timerMonitorScreen = value;
                base.OnPropertyChanged("TimerMonitorScreen");
            }
        }

        private ushort _timerHeartbeat;
        /// <summary>
        /// 心跳间隔秒数
        /// </summary>
        public ushort TimerHeartbeat
        {
            get { return _timerHeartbeat; }
            set
            {
                if (_timerHeartbeat == value) return;
                _timerHeartbeat = value;
                base.OnPropertyChanged("TimerHeartbeat");
            }
        }


        /// <summary>
        /// 确认
        /// </summary>
        [Display(GroupName = "DelegateCommand")]
        public DelegateCommand Cmd_Apply
        {
            get
            {
                if (_cmdApply == null)
                {
                    _cmdApply = new DelegateCommand(_apply, _canApply);
                }
                return _cmdApply;
            }
        }

        /// <summary>
        /// 取消
        /// </summary>
        [Display(GroupName = "DelegateCommand")]
        public DelegateCommand Cmd_Cancel
        {
            get
            {
                if (_cmdCancel == null)
                {
                    _cmdCancel = new DelegateCommand(_cancel, _canCancel);
                }
                return _cmdCancel;
            }
        }

        /// <summary>
        /// 重置
        /// </summary>
        [Display(GroupName = "DelegateCommand")]
        public DelegateCommand Cmd_Reset
        {
            get
            {
                if (_cmdReset == null)
                {
                    _cmdReset = new DelegateCommand(_reset, _canReset);
                }
                return _cmdReset;
            }
        }

        #endregion

        #region 方法

        #region DelegateCommand

        private bool _canApply()
        {
            return true;
        }

        private bool _canCancel()
        {
            return true;
        }

        private bool _canReset()
        {
            return true;
        }


        /// <summary>
        /// 确认
        /// </summary>
        private void _apply()
        {
            // 重新写入全局静态变量保存
            EnvUtilities.TabCountMonitorScreen = _tabCountMonitorScreen;
            EnvUtilities.TimerMonitorScreen = _timerMonitorScreen;
            EnvUtilities.TimerHeartbeat = _timerHeartbeat;

            // 修改 ini 文件
            INIOperation iniOperation = new INIOperation();
            string iniPath = string.Format(@"{0}\{1}\{2}.ini", Environment.CurrentDirectory,
                "DataBase", Utilities.DatabaseName);
            iniOperation.WriteValue(EnvUtilities.INI_SECTIONNAME_MONITORSCREEN,
                    EnvUtilities.INI_KEYNAME_MONITORSCREEN_TABCOUNT, _tabCountMonitorScreen.ToString(), iniPath);
            iniOperation.WriteValue(EnvUtilities.INI_SECTIONNAME_MONITORSCREEN,
                EnvUtilities.INI_KEYNAME_MONITORSCREEN_INTERVAL, _timerMonitorScreen.ToString(), iniPath);
            iniOperation.WriteValue(EnvUtilities.INI_SECTIONNAME_HEARTBEAT,
                EnvUtilities.INI_KEYNAME_HEARTBEAT_INTERVAL, _timerHeartbeat.ToString(), iniPath);

            AutoCloseNoticeWindow.Show("保存成功，重启程序后生效！", false);
            this.CloseCommand.Execute(null);
        }

        /// <summary>
        /// 取消
        /// </summary>
        private void _cancel()
        {
            TabCountMonitorScreen = EnvUtilities.TabCountMonitorScreen;
            TimerMonitorScreen = EnvUtilities.TimerMonitorScreen;
            TimerHeartbeat = EnvUtilities.TimerHeartbeat;

            this.CloseCommand.Execute(null);
        }

        /// <summary>
        /// 重置
        /// </summary>
        private void _reset()
        {
            TabCountMonitorScreen = EnvUtilities.TABCOUNT_MONITORSCREEN_DEFAULT;
            TimerMonitorScreen = EnvUtilities.TIMER_MONITORSCREEN_DEFAULT;
            TimerHeartbeat = EnvUtilities.TIMER_HEARTBEAT_DEFAULT;
        }

        #endregion

        #endregion
    }
}
